import { Alert } from 'react-native'
import { default as Config } from '../Config';
//JSON
function parseJson(response) {
    return response.json();
}

//判断状态
function checkStatus(response) {
    if (response.status >= 200 && response.status < 300) {
        console.log("res Content type: ", response.headers.get('content-type'));
        return response;
    }
    // debugger
    console.log(response.headers.TOKEN);
    //Alert.alert('' + response.status, response.url, [{ text: '确定' }]);
    const error = new Error(response.statusText);
    error.response = response;
    throw error;
}

function post(url, body = {} ,header={}) {
    const config = {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "charset": 'UTF-8',
            ...header
        },
        body: body,
        credentials: "include"
    }
    let newUrl = Config.baseUrl + url;
    console.log(newUrl)
    return fetch(newUrl, config)
        .then(response => response.json())
        .then(data => {console.log(data);
            return data;
        })
        .catch(err => { })
}


export default {post};